<template>
  <div>
    <a-button @click="test">测试</a-button>
    hhhhh
    <a-tree
      :tree-data="stationTree"
      :replaceFields="{ title: 's_name', key: 'uid' }"
    ></a-tree>
    kjjjjj
  </div>
</template>
<script>
var boxs = require("./box.json");
var stations = require("./station.json");
export default {
  data() {
    return {
      stationTree: [],
      state: {
        stationCache: {},
        modelCache: {},
        stationTree: [],
      },
    };
  },
  methods: {
    test() {
      this.stationTree = [];
      this.setChildStation({ parentId: "", children: stations.result });
      this.setThings({ res: boxs.result });
    },
    setChildStation({ parentId, children }) {
      //console.log("**********", rootthis.state)
      if (children) {
        children.sort((a, b) => {
          return a.s_name < b.s_name;
        });

        children.forEach((c) => {
          c.name = c.s_name;
          c.label = c.s_name;
          c.id = c.uid;
          this.state.stationCache[c.uid] = c;
          c.model = this.state.modelCache[c.model_id];
          if (c.model) {
            c.isLeaf = !c.model.is_station;
          }
        });
        let tree = [];
        children.forEach((c) => {
          if (!c.parent_id) {
            tree.push(c);
          } else {
            let p = this.state.stationCache[c.parent_id];
            if (p) {
              if (!p.children) {
                p.children = [];
                p.children2 = new Set()
              }
              if(!p.children2.has(c.uid)){
                  p.children.push(c);
                  p.children2.add(c.uid)
              }
              
            }
          }
        });

        if (parentId === "") {
          this.state.stationTree = tree;
        } else {
          let p = this.state.stationCache[parentId];
          if (p) {
            p.children = tree;
          }
        }
      }
    },
    setThings({ res, filterFun }) {
      let things = res;
      if (things) {
        let cache = {};
        let stationtree = [];
        if (filterFun) {
          things = things.filter((t) => {
            if (t.is_station) {
              return true;
            }
            return filterFun(t);
          });
        }
        things.forEach((t) => {
          t.key = t.uid;
          t.name = t.s_name;
          t.label = t.s_name;
          t.id = t.uid;
          t.data = t;
          cache[t.uid] = t;
          t.name_ip = t.s_name;
          if (t.ip) {
            t.name_ip += "@" + t.ip;
          }
        });
        var first;
        things.forEach((t) => {
          if (!t.is_station) {
            let station = cache[t.station_id];
            if (station) {
              if (!station.children) {
                station.children = [];
              }
              station.children.push(t);
              t.parent = station;
              if (t.model_id == "m_smartBoxForPolice") {
                first = t;
              }
            }
          } else {
            if (
              t.parent_id == "" ||
              t.parent_id == undefined ||
              t.parent_id == null ||
              t.parent_id == "null"
            ) {
              stationtree.push(t);
              if (!t.children) {
                t.children = [];
              }
            } else {
              let parent = cache[t.parent_id];
              if (parent) {
                if (!parent.children) {
                  parent.children = [];
                }
                parent.children.push(t);
                t.parent = parent;
              }
            }
          }
        });
        for (let item in cache) {
          let t = cache[item];

          if (t.is_station) {
            continue;
          }
          if (t.box_id) {
            let box = cache[t.box_id];
            if (box) {
              if (!box.cameras) {
                box.cameras = [];
              }
              box.cameras.push(t);
              continue;
            }
          }
          let parent = t.parent;
          if (!t.position) {
            t.position = [t.s_name];
          }
          while (parent) {
            t.position.unshift(parent.s_name);
            if (parent == parent.parent) {
              parent.parent = undefined;
            }
            parent = parent.parent;
          }
          //console.log(t.position)
        }
        this.state.thingCache = cache;
        if (filterFun) {
          this.state.treeFiltered = stationtree;
        } else {
          this.state.stationTree = stationtree;
        }

        this.state.first = first;
        this.stationTree = this.state.stationTree;
      }
    },
    setThing({ parentId, things }) {
      //console.log("**********", rootthis.state)
      if (things) {
        things.sort((a, b) => {
          return a.s_name < b.s_name;
        });
        things.forEach((t) => {
          t.name = t.s_name;
          t.name_ip = t.s_name;
          if (t.ip) {
            t.name_ip += "@" + t.ip;
          }
          t.id = t.uid;
          t.data = t;
        });

        let p = this.state.stationCache[parentId];
        if (p) {
          p.children = [];
          things.forEach((t) => {
            t.type = "obj";
            t.isLeaf = true;
            this.state.thingCache[t.uid] = t;
            p.children.push(t);
            t.model = this.state.modelCache[t.model_id];
            if (t.model) {
              t.isLeaf = !t.model.is_station;
            }
          });
        }
      }
      this.state.stationTree = [...this.state.stationTree];

    },
  },
};
</script>